package org.example.controller.admin;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.example.constant.JwtClaimsConstant;
import org.example.dto.EmployeeLoginDTO;
import org.example.entity.Employee;
import org.example.properties.JwtProperties;
import org.example.result.Result;
import org.example.service.DingdanService;
import org.example.service.EmployeeService;
import org.example.utils.JwtUtil;
import org.example.vo.EmployeeLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "用户相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private DingdanService dingdanService;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("用户登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);
        if(employee!=null) {
            //登录成功后，生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
            String token = JwtUtil.createJWT(
                    jwtProperties.getAdminSecretKey(),
                    jwtProperties.getAdminTtl(),
                    claims);

            EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                    .id(employee.getId())
                    .userName(employee.getUsername())
                    .name(employee.getName())
                    .token(token)
                    .build();
            return Result.success(employeeLoginVO);
        }
        return Result.success(null);
    }

    /**
     * 注册账户
     * @param employee
     * @return
     */
    @PostMapping("/zhuche")
    @ApiOperation(value = "用户注册")
    public Result<Integer> zhuche(@RequestBody Employee employee) {
        log.info("注册：{}", employee);

        if(employeeService.loginid(employee)==0) {
            employeeService.loginadd(employee);
            return Result.success(0);
        }else{

            return Result.success(1);
        }
    }


    /**
     * 忘记密码
     * @param employee
     * @return
     */
    @PutMapping("/wangji")
    @ApiOperation(value = "用户忘记密码")
    public Result<Integer> wangji(@RequestBody Employee employee) {
        log.info("忘记密码：{}", employee);
        if(employeeService.loginid(employee)!=0){
            employeeService.loginwangji(employee);

            return Result.success(1);
        }
        return Result.success(0);
    }

    /**
     * 查询
     * @return
     */
    @GetMapping ("/chaxun")
    @ApiOperation(value = "用户查询")
    public Result<Employee> chaxun(@RequestParam( "username") String username) {

        log.info("查询：{}", username);

        return Result.success( employeeService.chaxun(username));
    }


    /**
     * 注销
     * @return
     */
    @DeleteMapping ("/zx")
    @ApiOperation(value = "用户查询")
    public Result zx(@RequestParam( "username") String username) {

        log.info("注销：{}", username);
        if(dingdanService.zxcx(username)==0) {
            employeeService.zx(username);
            return Result.success();
        }
        return Result.error("失败");
    }

}
